<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="编辑定时任务"
        width="560px"
    >
        <div :loading="loading" class="edit_page_div">
            <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px">
                <el-form-item label="任务名称" prop="taskName">
                    <el-input v-model="form.taskName" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="包名称" prop="beanName">
                    <el-input v-model="form.beanName" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="方法名称" prop="methodName">
                    <el-input v-model="form.methodName" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="表达式" prop="cronValue">
                    <el-input v-model="form.cronValue" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="方法参数" prop="paramContent">
                    <el-input v-model="form.paramContent" style="width: 400px;"></el-input>
                </el-form-item>
            </el-form>
            <div class="edit_button">
                <el-button type="primary" @click="submit" v-loading.fullscreen.lock="loading">保存</el-button>
                <el-button type="primary" @click="resetForm">取消</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
export default {
    name: "scheduledEdit",
    props: {
        visible: Boolean,
        id: String,
    },
    data() {
        return {
            loading: false,
            form: {
                taskName: '',
                beanName: '',
                methodName: '',
                paramContent: '',
                cronValue: '',
                enableFlag: 0,
            },
            rules: {
                taskName: [{required: true, message: '', trigger: 'change'}],
                beanName: [{required: true, message: '', trigger: 'change'}],
                methodName: [{required: true, message: '', trigger: 'change'}],
                cronValue: [{required: true, message: '', trigger: 'change'}],
                paramContent: [{required: false, message: '', trigger: 'change'}],
            }
        }
    },
    watch: {
        visible(newval) {
            if (newval) {
                console.log(this.id)
                if (this.id) {
                    this.getData();
                }
            }
        }
    },
    methods: {
        getData() {
            this.loading = true;
            this.$axios.get("/CHILD_SYSTEM/baseScheduled/getOneEntity/" + this.id).then(res => {
                this.form = res.data;
            }).finally(() => {
                this.loading = false;
            })
        },
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/baseScheduled/" + (this.id ? "update" : "insert"), this.form).then(res => {
                        this.resetForm();
                        this.$message.success(res.message);
                    }).finally(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        }
    }
}
</script>

<style scoped>

</style>